/**
 * Created by IntelliJ IDEA.
 * User: akolonitsky
 * Date: 2/1/12
 * Time: 7:40 PM
 * To change this template use File | Settings | File Templates.
 */
package model.service
{
    import flash.events.Event;
    import flash.events.IEventDispatcher;
    import flash.net.URLRequestMethod;

    import org.kolonitsky.alexey.net.ElfinSerivce;
    import org.kolonitsky.alexey.utils.DateUtil;

    /**
     * Complete event dispatched when operation is complete.
     */
    [Event(name="complete", type="flash.events.Event")]

    public class CurrencyRateService implements IEventDispatcher, IElfinService
    {
        public function CurrencyRateService()
        {
            service = new ElfinSerivce("", this);
        }




        //---------------------------------------------------------------------
        //
        //   Public
        //
        //---------------------------------------------------------------------


        public function getCurrencyRates(currencies:Array = null, fromDate:Date=null,
            toDate:Date=null):void
        {
            var params:Object = {};

            if (currencies && currencies.length > 0)
                params.currencies = currencies.join(",");

            if (fromDate)
                params.from = DateUtil.dateToUnix(fromDate);

            if (toDate)
                params.to = DateUtil.dateToUnix(toDate)

            service.load(Config.CURRENCIES_GET, URLRequestMethod.GET, params);
        }

        public function get rates():Array
        {
            return service.lastData as Array;
        }




        //---------------------------------------------------------------------
        //
        //   Implement IElfinService
        //
        //---------------------------------------------------------------------


        public function init(host:String, authenticityToken:String):void
        {
            service.baseURL = host;
            service.authenticityToken = authenticityToken;
        }

        public function load(url:*, type:String=URLRequestMethod.GET, parameters:Object=null):void
        {
            service.load(url,  type,  parameters);
        }

        public function clear():void
        {
            service.clear();
        }

        //---------------------------------------------------------------------
        //
        //   Implement IEventDispatcher
        //
        //---------------------------------------------------------------------

        public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
        {
            service.removeEventListener(type, listener,  useCapture);
        }

        public function hasEventListener(type:String):Boolean
        {
            return service.hasEventListener(type);
        }

        public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
        {
            service.addEventListener(type, listener, useCapture, priority, useWeakReference)
        }

        public function dispatchEvent(event:Event):Boolean
        {
            return service.dispatchEvent(event);
        }

        public function willTrigger(type:String):Boolean
        {
            return service.willTrigger(type);
        }


        private var service:ElfinSerivce;
    }
}
